ConfirmPurchase = new Object();

ConfirmPurchase.getBasicFieldSet = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Basic',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Fund',
				inputValue : 'confirmPurchaseFund',
				id : 'confPurchaseFund',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Name',
				inputValue : 'confirmPurchInsName',
				id : 'confPurchaseInsName',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Value Date',
				inputValue : 'confirmPurchValueDt',
				id : 'confPurchaseValueDt',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Matured Date',
				inputValue : 'confirmPurchMaturityDt',
				id : 'confPurchaseMaturityDt',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Delevary Method',
				inputValue : 'confPurchaseDelevaryMethod',
				id : 'confPurchaseDelevaryMethod',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Dealer',
				inputValue : 'confPurchaseDealer',
				id : 'confPurchaseDealer',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'checkboxfield',
			boxLabel : 'Settlement Method',
			inputValue : 'confPurchaseSettlementMethod',
			id : 'confPurchaseSettlementMethod',
			flex : 1,
			padding : '0 10 0 10',
		} ]
	});
};

ConfirmPurchase.getInstrumentFieldSet = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Instrument Type',
				inputValue : 'confirmPurchInstrumentType',
				id : 'confPurchaseInstrumentType',
				flex : 1,
				// checked:true,
				// disabled:true,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Instrument Code',
				inputValue : 'confirmPurchInstrumentCodeCombo;confirmPurchInstrumentCodeText',
				id : 'confPurchaseInstrumentCode',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, ]
	});
};

ConfirmPurchase.getDealFieldSet = function() {
	return Ext.create('Ext.form.FieldSet', {
		title : 'Deal',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Deal Value',
				inputValue : 'confirmPurchDealVal',
				id : 'confPurchaseDealVal',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Placement Fee',
				inputValue : 'confirmPurchPlacementFee',
				id : 'confPurchasePlacementFee',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : 'Secondary Market',
				inputValue : 'confirmPurchSecMarket',
				id : 'confPurchaseSecMarket',
				padding : '0 10 0 10',
				flex : 1,
			}, {
				id : 'confPurchaseMaturityValue',
				boxLabel : 'Maturity Value',
				inputValue : 'confirmPurchMaturityValue',
				padding : '0 10 0 10',
				flex : 1,
			} ],
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Seller's Name",
				inputValue : 'secSellerName',
				id : 'confPurchasesecSellerName',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Bank No',
				inputValue : 'secSellerBank',
				id : 'confPurchasesecSellerBank',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		} ]
	});
};

ConfirmPurchase.getInstrumentDetailFieldSet = function() {

	return Ext.create('Ext.form.FieldSet', {
		title : 'Instrument Details',
		defaults : {
			anchor : '100%'
		},
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Issuer",
				inputValue : 'recordDealPrimDealer;recordDealIssuer',
				id : 'confPurchasesecIssuer',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Withholding Tax',
				inputValue : 'withholdingTaxRate',
				id : 'confPurchaseWithholdingTaxRate',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Interest Rate",
				inputValue : 'confirmPurchIntRate',
				id : 'confPurchaseIntRate',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Day Basis',
				inputValue : 'confirmPurchDayBasis',
				id : 'confPurchaseDayBasis',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Coupon Rate",
				inputValue : 'confirmPurchCouponRate',
				id : 'confPurchaseCouponRate',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Coupon Dates',
				inputValue : 'confirmPurchCpnDts',
				id : 'confPurchaseCpnDts',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Reset Period (Floating Rates)",
				inputValue : 'RecPurchResetPeriod',
				id : 'confPurchaseResetPeriod',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Reset Date',
				inputValue : 'confirmPurchFloatResetDt',
				id : 'confPurchaseFloatResetDt',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Floor Value",
				inputValue : 'confirmPurchFloor',
				id : 'confPurchaseFloor',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Ceiling Value',
				inputValue : 'confirmPurchCeiling',
				id : 'confPurchaseCeiling',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			defaultType : 'checkboxfield',
			items : [ {
				boxLabel : "Bench Mark",
				inputValue : 'confirmPurchBench',
				id : 'confPurchaseBench',
				flex : 1,
				padding : '0 10 0 10',
			}, {
				boxLabel : 'Minimum Denominal Value',
				inputValue : 'confirmPurchDenominamMinVal',
				id : 'confPurchaseDenominamMinVal',
				flex : 1,
				padding : '0 10 0 10',
			} ]
		} ]
	});
};

ConfirmPurchase.getCenterPanel = function() {

	var basic = ConfirmPurchase.getBasicFieldSet();
	var instrument = ConfirmPurchase.getInstrumentFieldSet();
	var deal = ConfirmPurchase.getDealFieldSet();
	var insDtl = ConfirmPurchase.getInstrumentDetailFieldSet();
	var formPanel = Ext.create('Ext.form.FormPanel', {
		title : 'Confirmation Fields for a Purchase Deal',
		bodyPadding : '20',
		autoScroll : true,
		frame : true,
		defaults : {
			anchor : '72%',
		},
		fieldDefaults : {
			labelAlign : 'left',
		},
		items : [ instrument, basic, deal, insDtl ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
				Ext.data.StoreManager.lookup('couponDatesStore').removeAll();
			}
		}, {
			text : 'Save',
			itemId : 'saveButton',
			handler : function() {
				var form = this.up('form').getForm();
				var valueSet = '';
				var localFieldSet = '';

				if (Ext.ComponentQuery.query('#confPurchaseFund')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseFund')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseFund;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseInsName')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseInsName')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseInsName;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseValueDt')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseValueDt')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseValueDt;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseMaturityDt')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseMaturityDt')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseMaturityDt;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseInstrumentType')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseInstrumentType')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseInstrumentType;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseInstrumentCode')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseInstrumentCode')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseInstrumentCode;confPurchaseInstrumentCode;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseDealVal')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseDealVal')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseDealVal;';
				}

				if (Ext.ComponentQuery.query('#confPurchasePlacementFee')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchasePlacementFee')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchasePlacementFee;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseSecMarket')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseSecMarket')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseSecMarket;';
				}

				if (Ext.ComponentQuery.query('#confPurchasesecSellerName')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchasesecSellerName')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchasesecSellerName;';
				}

				if (Ext.ComponentQuery.query('#confPurchasesecSellerBank')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchasesecSellerBank')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchasesecSellerBank;';
				}

				if (Ext.ComponentQuery.query('#confPurchasesecIssuer')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchasesecIssuer')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchasesecIssuer;confPurchasesecIssuer;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseWithholdingTaxRate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseWithholdingTaxRate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseWithholdingTaxRate;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseIntRate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseIntRate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseIntRate;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseDayBasis')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseDayBasis')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseDayBasis;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseCouponRate')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseCouponRate')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseCouponRate;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseCpnDts')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseCpnDts')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseCpnDts;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseResetPeriod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseResetPeriod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseResetPeriod;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseFloatResetDt')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseFloatResetDt')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseFloatResetDt;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseFloor')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseFloor')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseFloor;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseCeiling')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseCeiling')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseCeiling;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseBench')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseBench')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseBench;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseDenominamMinVal')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseDenominamMinVal')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseDenominamMinVal;';
				}

				if (Ext.ComponentQuery.query('#confPurchaseMaturityValue')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseMaturityValue')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseMaturityValue;';
				}
				if (Ext.ComponentQuery.query('#confPurchaseSettlementMethod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseSettlementMethod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseSettlementMethod;';
				}
				if (Ext.ComponentQuery.query('#confPurchaseDealer')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseDealer')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseDealer;';
				}
				if (Ext.ComponentQuery.query('#confPurchaseDelevaryMethod')[0].getValue()) {
					valueSet = valueSet + Ext.ComponentQuery.query('#confPurchaseDelevaryMethod')[0].getSubmitValue() + ';';
					localFieldSet = localFieldSet + 'confPurchaseDelevaryMethod;';
				}

				form.url = 'newConfField.action';

				formPanel.setLoading('Saving...');
				form.submit({

					params : {
						'screen' : 'RecordPurchase',
						'values' : valueSet,
						'localFields' : localFieldSet,
						'user' : 'Confirm User',

					},
					success : function(form, action) {
						// form.reset();
						formPanel.setLoading(false);
						Ext.Msg.alert('Success', 'Fields Saved!.');
					},
					failure : function(form, action) {
						Ext.Msg.alert('Failed!', 'There is some error returned from the server.');
						formPanel.setLoading(false);
					}
				});

			}

		} ],
	// listeners : {
	// 'afterrender' : {
	// fn : function(thisField) {
	// thisField.setLoading('Receiving configs...');
	// }
	// }
	// }
	});

	Ext.Ajax.request({
		url : 'confLocalFields.action',
		params : {
			screen : 'RecordPurchase',
		},
		success : function(response) {
			var obj = Ext.decode(response.responseText);
			confirmFields = obj.values;

			var confFieldsArrray = confirmFields.split(";");
			var i;
			var confFieldLocal;
			var confFieldQuery;
			for (i = 0; i < confFieldsArrray.length - 1; i++) {
				confFieldQuery = '#' + confFieldsArrray[i];
				confFieldLocal = Ext.ComponentQuery.query(confFieldQuery)[0];
				confFieldLocal.setValue(true);
			}
			// formPanel.setLoading(false);
		},
		failure : function(form, action) {
			// formPanel.setLoading(false);
			Ext.Msg.alert('Error!', 'Failed to load Confirmation Fields!');
		}
	});

	return formPanel;
};
